package com.wzg.mmkv;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.wzg.logrlibrary.LogR;
import com.wzg.mmkv.entity.User;
import com.wzg.mmkv.sample.R;
import com.wzg.sputils.SPUtil;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {



    EditText mEditText;
    TextView showText;

    MMKVUtil  mmkvUtil;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mmkvUtil=APP.getMmkvUtil();

        mEditText = findViewById(R.id.show1);
        showText = findViewById(R.id.show2);

        findViewById(R.id.test1).setOnClickListener(v -> {
            SPUtil.clear();
            long time = System.currentTimeMillis();
            for (int i = 0; i < 1000; i++) {
                SPUtil.putBoolean("mmkv_test" + i, true);
                SPUtil.putString("mmkv_test_str_" + i, "测试内容" + i);
                LogR.logDateI( "" + SPUtil.getBoolean("mmkv_test" + i));
                LogR.logDateI( "" + SPUtil.getString("mmkv_test_str_" + i));
            }
            showText.setText(String.format(Locale.getDefault(), "SPUtils运行时间：%d毫秒", System.currentTimeMillis() - time));
        });

        //migrate
        findViewById(R.id.test2).setOnClickListener(v -> {

            /*进行数据迁移*/
            mmkvUtil.setMigrate(true);

//            KVUtils.getInstance().clear();
//            long time = System.currentTimeMillis();
//            for (int i = 0; i < 1000; i++) {
//               mmkvUtil.putBoolean("mmkv_test" + i, true);
//               mmkvUtil.putString("mmkv_test_str_" + i, "测试内容" + i);
//               LogR.logDateI("" + mmkvUtil.getBoolean("mmkv_test" + i));
//               LogR.logDateI( "" +mmkvUtil.getString("mmkv_test_str_" + i));
//            }
//            showText.setText(String.format(Locale.getDefault(), "KVUtils运行时间：%d毫秒", System.currentTimeMillis() - time));
        });
        //get    查看是否数据迁移成功
        findViewById(R.id.button2).setOnClickListener(v -> {
//            String value = KVUtils.getInstance().getString("mmkv_key");
            String value = mmkvUtil.getString("mmkv_test_str_999");
            showText.setText("" + value);
        });
        //clear
        findViewById(R.id.button3).setOnClickListener(v -> mmkvUtil.clear());

        //save
        findViewById(R.id.button1).setOnClickListener(v -> {
            String value = mEditText.getText().toString();
            mmkvUtil.putString("mmkv_key", value);
        });


        User  userTom=new User();
        userTom.setSex("女v15");
        userTom.setUserName("userTome");
        userTom.setPwd("v15pwd");
        userTom.setWorkID(1+"");

        mmkvUtil.putObjectData(userTom);


        LogR.logDateE("user==\t"+mmkvUtil.getObjectData(User.class));


//        mmkvUtil.clear();
        mmkvUtil.deleteAppointObjectData(User.class);


        LogR.logDateE("user==\t"+mmkvUtil.getObjectData(User.class));







    }


}
